package com.dongdongshop.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.mapper.TbTypeTemplateMapper;
import com.dongdongshop.page.PageBeen;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@org.springframework.stereotype.Service
public class GoodsServiceImp implements GoodsService {
    @Autowired
    private TbGoodsMapper tbGoodsMapper;
    @Autowired
    private TbGoodsDescMapper tbGoodsDescMapper;
    @Autowired
    private TbTypeTemplateMapper tbTypeTemplateMapper;
    @Autowired
    private TbItemMapper tbItemMapper;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public PageBeen<TbGoods> selectGoods(String goodsName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TbGoods> list = tbGoodsMapper.selectGood(goodsName);
        PageInfo<TbGoods> pageInfo = new PageInfo<>(list);
        PageBeen pageBeen = new PageBeen(pageNum, pageSize, pageInfo.getTotal(), pageInfo.getList(), pageInfo.getPages());
        return pageBeen;
    }

    @Override
    public int deleteInfoGoods(Long[] ids) {
        return tbGoodsMapper.deleteInfoGoods(ids);
    }

    @Override
    public int update(String auditStatus, Long[] ids) {
        int i = tbGoodsMapper.updatePass(auditStatus, ids);
        rocketMQ(ids);
        return i;
    }

    public Integer rocketMQ(Long[] ids) {
        for (int i = 0; i <ids.length ; i++) {
            String s = ids[i].toString();
            rocketMQTemplate.convertAndSend("audit_status", s);//审核是否通过,对商家进行同步
        }
        return 1;
    };

    @Override
    public int deleteInfoDelete(String isDelete, Long[] ids) {
        return tbGoodsMapper.updateDelete(isDelete, ids);
    }

    @Override
    public TbGoods selectGoodsById(Long goodsId) {
        return tbGoodsMapper.selectByPrimaryKey(goodsId);
    }

    @Override
    @Transactional
    public int addAllInfo(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc, List<TbItem> itemList, String name, String thp1, String thp2) {
        int i3 = tbGoodsMapper.addAllInfo(tbGoods);
        tbGoodsDesc.setGoodsId(tbGoods.getId());
        int i1 = tbGoodsDescMapper.insertSelective(tbGoodsDesc);
        for (int i = 0; i < itemList.size(); i++) {
            TbItem ti = itemList.get(i);
            String spec = ti.getSpec();
            String thp11 = spec.replace("thp1", thp1);
            if (thp2 != null && thp2 != "") {
                String thp21 = thp11.replace("thp2", thp2);
                ti.setSpec(thp21);
            }else {
                ti.setSpec(thp11);
            }
            ti.setSellerId(tbGoods.getSellerId());
            ti.setSeller(name);
            ti.setGoodsId(tbGoods.getId());
            ti.setSellPoint("非常好,好极了");
            int i2 = tbItemMapper.insertSelective(ti);
        }
        return 1;
    }
}
